<template>
  <div>
    <el-dialog :close-on-click-modal="false" id="register" title="报名注册" :visible.sync="dialogAdd" width="700px"
      @close="close">
      <div class="register-title">
        如果您已经有平台账号请先
        <router-link to="login">登录</router-link>
      </div>
      <el-form class="form-add" label-width="100px">
        <el-form-item class="long-input" label="姓名：" prop="name">
          <el-input v-model="formAdd.name" maxlength="10" show-word-limit placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item class="long-input mb0" label="手机号：" prop="name">
          <el-input v-model="formAdd.telephoneNumber" maxlength="11" show-word-limit placeholder="请输入手机号"></el-input>
          <div>注：手机号将作为您的账号使用</div>
        </el-form-item>
        <el-form-item class="long-input" label="专业：" prop="name">
          <el-input v-model="formAdd.major" maxlength="50" show-word-limit placeholder="请输入专业"></el-input>
        </el-form-item>
        <el-form-item class="long-input" label="班级：" prop="name">
          <el-input v-model="formAdd.className" maxlength="50" show-word-limit placeholder="请输入班级"></el-input>
        </el-form-item>
        <el-form-item class="long-input" label="密码：" prop="name">
          <el-input type="password" v-model="formAdd.password" maxlength="16" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="long-input" label="确认密码：" prop="name">
          <el-input type="password" v-model="rePassword" maxlength="16" placeholder="请确认密码"></el-input>
        </el-form-item>
        <el-form-item class="register-yzmbox" label="验证码：" prop="name">
          <el-input v-model="formAdd.verificationCode" maxlength="4" show-word-limit placeholder="请输入验证码"></el-input>
          <div @click="getImageCode" class="register-yzm">
            <img :src="imageCode" alt="" />
          </div>
        </el-form-item>
      </el-form>
      <div class="footer" style="text-align: center">
        <button class="button-style saveBtn" @click="onSubmit">确 认</button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const _ = require("lodash");
export default {
  data() {
    return {
      isSign:false,
      coureId: "",
      imageCode: "",
      rePassword: "",
      //添加选手
      formAdd: {
        className: "",
        major: "",
        name: "",
        password: "",
        telephoneNumber: "",
        verificationCode: "",
      },
      dialogAdd: false,
    };
  },
  created() {
    this.courseId = this.$route.query.courseId;
    console.log(this.courseId, 215);
    this.init();
  },
  methods: {
    init() {
      this.getImageCode();
    },
    //注册提交
    onSubmit() {
      let url = "";
      console.log(this.isSign, 23456);
      if (this.isSign) {
        //注册并报名
        url = `/tes/unauthorized/apply/person/${this.courseId}`;
      } else {
        //仅注册
        url = "/tes/unauthorized/register";
      }
      //两次输入密码不一致
      if (this.formAdd.password !== this.rePassword) {
        this.$message.error("两次输入密码不一致");
        return;
      }
      const loading = this.$loading({
        lock: true,
        text: "加载中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.$http
        .post(url, this.formAdd)
        .then((response) => {
          let res = response.data;
          if (res.code == "200") {
            this.$message.success(res.message);
            this.dialogAdd = false;
            if(this.isSign){
              this.$parent.showSuccessDialog();
            }
          } else {
            this.$message.error(res.message);
          }
          loading.close();
        })
        .catch((err) => {
          console.log(err);
          loading.close();
        });
    },
    getImageCode() {
      this.imageCode =
        "/tes/unauthorized/image-code?width=75&height=35&time=" + new Date().valueOf();
    },
    close() {
      this.dialogAdd = false;
      this.clearList();
    },
    //清除
    clearList() {
      this.formAdd = {
        className: "",
        major: "",
        name: "",
        password: "",
        telephoneNumber: "",
        verificationCode: "",
      };
      this.rePassword = "";
    },
    add() {
      this.dialogAdd = true;
    },
  },
};
</script>
<style lang="scss">
#register {
  .el-input {
    .el-input__inner {
      width: auto;
      padding-right: 47px;
    }
  }

  .register-title {
    font-size: 18px;
    text-align: center;
    line-height: 30px;
    margin-bottom: 26px;
  }

  .form-add {
    .el-input {
      width: auto;

      .el-input__count-inner {
        background: none;
      }
    }
  }

  .long-input {
    .el-input__inner {
      width: 500px;
    }
  }

  .el-form-item__content {
    height: auto;
  }

  .mb0 {
    margin-bottom: 0;
  }

  .register-yzmbox {
    position: relative;

    >div {
      width: 215px;
    }

    .register-yzm {
      height: 100%;
      position: absolute;
      top: 0;
      right: -80px;

      img {
        height: 80%;
      }
    }
  }
}
</style>
